﻿using System;
using System.Web.Security;
using BestEasyCMS.Framework.CookieStorage;

namespace BestEasyCMS.Framework.Authentication
{
    public class AspFormsAuthentication : IFormsAuthentication
    {
        private readonly ICookieStorageService _cookieStorageService;
        public AspFormsAuthentication(ICookieStorageService cookieStorageService)
        {
            _cookieStorageService = cookieStorageService;
        }

        public void SetAuthenticationToken(string token)
        {
            FormsAuthentication.SetAuthCookie(token, false);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        public void SetAuthenticationToken(string userName, int expiration, string roleName)
        {
            var authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
                new DateTime().AddMinutes(expiration), false, roleName);
            var encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            _cookieStorageService.AddCookie(FormsAuthentication.FormsCookieName, encryptedTicket,
                new DateTime().AddMinutes(expiration));
        }
    }

}
